 import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class SNN {

	
	public static void main(String[] args){
		List<titanic> tlist= new ArrayList<>();
		List<titanic> trainList= new ArrayList<>();
		List<titanic> testList= new ArrayList<>();
		Random r = new Random();
		int neighborSize = 7;
		double maxclas = -99;
		double maxage = -99;
		double maxsex = -99;
		double minclas = 99;
		double minage = 99;
		double minsex = 99;
		FileReader reader;
		try {
			reader = new FileReader("d:/titanic.dat");
			BufferedReader br = new BufferedReader(reader);
			String str = null;
		
			int i = 0;
			while((str = br.readLine())!=null){
				//System.out.println(str);
				if(i>7){
					String splarray [] = str.split(",");
					titanic tc = new titanic();
					//range
					double tempclas = Double.parseDouble(splarray[0]);
					double tempage = Double.parseDouble(splarray[1]);
					double tempsex = Double.parseDouble(splarray[2]);
					double tempsurvived = Double.parseDouble(splarray[3]);
					if(i ==8){
						maxclas = tempclas;
						maxage = tempage;
						maxsex = tempsex;
					}
					tc.setclas(tempclas);
					tc.setAge(tempage);
					tc.setSex(tempsex);
			
					tc.setSurvived(tempsurvived);
					tlist.add(tc);
					if(maxclas<tempclas){
						maxclas = tempclas;
					}
					if(maxage<tempage){
						maxage = tempage;
					}
					if(maxsex<tempsex){
						maxsex = tempsex;
					}
					if(minclas>tempclas){
						minclas = tempclas;
					}
					if(minage>tempage){
						minage = tempage;
					}
					if(minsex>tempsex){
						minsex = tempsex;
					}
				}
					i++;
			}
		} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		}
		System.out.println(maxclas+" "+maxage+" "+maxsex);
		System.out.println(minclas+" "+minage+" "+minsex);
		//normalize
		for(int i=0;i<tlist.size();i++){
			titanic temptitanic = (titanic)tlist.get(i);
			tlist.remove(i);
			temptitanic.setclas(normalize(maxclas,minclas,temptitanic.getclas()));
			temptitanic.setAge(normalize(maxage,minage,temptitanic.getAge()));
			temptitanic.setSex(normalize(maxsex,minsex,temptitanic.getSex()));
//			System.out.println(temptitanic.getclas());
			tlist.add(temptitanic);		
			if(r.nextDouble()<0.7){
				trainList.add(temptitanic);
			}else{
				testList.add(temptitanic);
			}
		}
		double rightSize = 0;
		double totalSize = (double)testList.size();
		for(int i=0;i<testList.size();i++){
			List<titanic> templist = new ArrayList<>();
			double class1 = 0;
			double class2 = 0;
			for(int j=0;j<trainList.size();j++){
				//calculate neighborSize shortest distance
				if(templist.size()<neighborSize){
					templist.add(trainList.get(j));
				}else{
					titanic longestTitanic=templist.get(0);
					for(int k=0;k<templist.size();k++){
						if(getdistance(testList.get(i),longestTitanic)<getdistance(testList.get(i),templist.get(k))){
							longestTitanic = templist.get(k);
						}
					}
					if(getdistance(testList.get(i),trainList.get(j))<getdistance(testList.get(i),longestTitanic)){
						templist.remove(longestTitanic);
						templist.add(trainList.get(j));
					}
					
				}
			}
			//count every class quantity
			for(int l=0;l<templist.size();l++){
				if(templist.get(l).getSurvived()==1.0){
					class1++;
				}else{
					class2++;
				}
			}
			//predict Class
			double predictClass = -1;
			if(class1>class2){
				predictClass=1.0;
			}else{
				predictClass=-1.0;
			}
			//judge predict and origin
			if(predictClass==testList.get(i).getSurvived()){
				rightSize++;
			}
		}
		System.out.println("correctness:"+rightSize/totalSize);
	}
	
	public static double normalize(double max,double min,double number){
		double temp = number-min;
		temp/=(max-min);
		return temp;
	}
	
	public static double getdistance(titanic t1,titanic t2){
		return Math.sqrt(Math.pow(t1.getclas()-t2.getclas(), 2)+Math.pow(t1.getAge()-t2.getAge(), 2)+Math.pow(t1.getSex()-t2.getSex(), 2));
	}
}